package com.sqx.modules.tbOrders.controller.app;

import com.sqx.common.annotation.SysLog;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.annotation.Login;
import com.sqx.modules.tbOrders.dao.ComplaintDao;
import com.sqx.modules.tbOrders.entity.SysComplaint;
import com.sqx.modules.tbOrders.entity.TbOrders;
import com.sqx.modules.tbOrders.service.ComplaintService;
import com.sqx.modules.tbOrders.service.TbOrdersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @description：TODO
 * @author： qiao
 * @create： 2024/7/9 14:03
 */
@RestController
@RequestMapping("/app/orders")
@Slf4j
@Api(value = "用户下单-用户端", tags = "用户下单-用户端")
public class AppTbOrdersController {

    @Autowired
    private TbOrdersService tbOrdersService;
    @Autowired
    private ComplaintService complaintService;

    private final ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock(true);

    @Login
    @PostMapping("/placeOrder")
    @ApiOperation("用户下单")
    public Result placeOrder(@ApiParam("用户id") @RequestAttribute Long userId,
                             @ApiParam("订单实体") TbOrders tbOrders) {
        if (tbOrders.getRetrieveType() == 0) {
            tbOrders.setSellerId(userId);
        } else {
            tbOrders.setBuyerId(userId);
        }
        reentrantReadWriteLock.writeLock().lock();
        try {
            return tbOrdersService.placeOrder(tbOrders);
        } catch (Exception e) {
            log.error("下单异常：" + e.getMessage(), e);
        } finally {
            reentrantReadWriteLock.writeLock().unlock();
        }
        return Result.error("系统繁忙，请稍后再试！");
    }

    @Login
    @GetMapping("/getAppTbOrdersList")
    @ApiOperation("获取用户端订单列表(我买到的)")
    public Result getAppTbOrdersList(@ApiParam("用户id") @RequestAttribute Long userId,
                                     @ApiParam("页码") Integer page,
                                     @ApiParam("条数") Integer limit,
                                     @ApiParam("订单实体") TbOrders tbOrders) {
        tbOrders.setBuyerId(userId);
        return tbOrdersService.getAppTbOrdersList(page, limit, tbOrders);
    }

    @Login
    @GetMapping("/getAppOrdersDetail")
    @ApiOperation("获取用户端订单详情如果评价了将评价一起返回")
    public Result getAppOrdersDetail(@ApiParam("用户id") @RequestAttribute Long userId,
                                     @ApiParam("订单id") Long orderId) {
        return tbOrdersService.getAppOrdersDetail(orderId, userId);
    }

//    @Login
//    @GetMapping("/getAppOrdersDetail")
//    @ApiOperation("获取用户端订单详情如果评价了将评价一起返回")
//    public Result getAppOrdersDetail(@RequestAttribute Long userId, Long orderId) {
//        return tbOrdersService.getAppOrdersDetail(orderId, userId);
//    }

    @Login
    @PostMapping("/appSendGoods")
    @ApiOperation("用户端卖家确认发货")
    public Result appSendGoods(@ApiParam("用户id") @RequestAttribute Long userId,
                               @ApiParam("订单id") Long orderId,
                               @ApiParam("快递单号") String trackingNumber) {
        return tbOrdersService.appSendGoods(userId, orderId, trackingNumber);
    }

    @Login
    @PostMapping("/appReceived")
    @ApiOperation("用户端买家确认收货")
    public Result appReceived(@ApiParam("用户id") @RequestAttribute Long userId,
                              @ApiParam("订单id") Long orderId) {
        return tbOrdersService.appReceived(userId, orderId);
    }

    /**
     * 用户端买家评价
     *
     * @param userId
     * @param orderId
     * @param evaluateMessage  评价内容
     * @param satisfactionFlag 评级 1:非常差 2:差 3:一般 4:满意 5:非常满意
     * @param evaluateImg      评价图片
     * @return
     */
    @Login
    @PostMapping("/appEvaluate")
    @ApiOperation("用户端买家评价")
    public Result appEvaluate(@ApiParam("用户id") @RequestAttribute Long userId,
                              @ApiParam("订单id") Long orderId,
                              @ApiParam("评价内容") String evaluateMessage,
                              @ApiParam("评级 1:非常差 2:差 3:一般 4:满意 5:非常满意") Integer satisfactionFlag,
                              @ApiParam("评价图片") String evaluateImg) {
        return tbOrdersService.appEvaluate(userId, orderId, evaluateMessage, satisfactionFlag, evaluateImg);
    }

    @Login
    @PostMapping("/appCancelOrder")
    @ApiOperation("用户端取消订单")
    public Result appCancelOrder(@ApiParam("用户id") @RequestAttribute Long userId,
                                 @ApiParam("订单id") Long orderId) {
        return tbOrdersService.appCancelOrder(userId, orderId);
    }

    @Login
    @PostMapping("/appCancelOrderByType")
    @ApiOperation("用户端取消回收订单")
    public Result appCancelOrderByType(@ApiParam("用户id") @RequestAttribute Long userId,
                                       @ApiParam("订单id") Long orderId) {
        return tbOrdersService.appCancelOrderByType(userId, orderId);
    }

    @Login
    @GetMapping("/getTbOrdersList")
    @ApiOperation("获取用户端订单列表(我卖出的)")
    public Result getTbOrdersList(@ApiParam("用户id") @RequestAttribute Long userId,
                                  @ApiParam("页码") Integer page,
                                  @ApiParam("条数") Integer limit,
                                  @ApiParam("订单实体") TbOrders tbOrders) {
        tbOrders.setSellerId(userId);
        return tbOrdersService.getTbOrdersList(page, limit, tbOrders);
    }


    @Login
    @GetMapping("/getTbOrdersListByStatus")
    @ApiOperation("获取二手回收订单的成交量和评论")
    public Result getTbOrdersListByStatus(@ApiParam("用户id") @RequestAttribute Long userId,
                                          @ApiParam("页码") Integer page,
                                          @ApiParam("条数") Integer limit) {
        return tbOrdersService.getTbOrdersListByTurnover(userId, page, limit);
    }

    @Login
    @PostMapping("/updateRecycleOrder")
    @ApiOperation("更新回收订单")
    public Result updateRecycleOrder(@ApiParam("订单实体") TbOrders tbOrders) {
        return tbOrdersService.updateRecycleOrder(tbOrders);
    }

    @Login
    @GetMapping(value = "/findAllComplaint")
    @ApiOperation("被投诉人查看自己的所有被投诉")
    public Result findAllComplaint(@ApiParam("用户id") @RequestAttribute Long userId,
                                   @ApiParam("投诉类型id") Integer illegalId,
                                   @ApiParam("本投诉状态（1可申诉；2申诉中；3申诉未通过；4申诉通过；5用户发起投诉；6用户投诉被拒绝）") Integer complaintState,
                                   @ApiParam("页码") Integer page,
                                   @ApiParam("条数") Integer limit,
                                   @ApiParam("用户名") String userName) {
        return complaintService.findAllComplaint(userId, illegalId, complaintState, page, limit, userName);
    }

    @Login
    @GetMapping(value = "/findComplaint")
    @ApiOperation("被投诉人查看被投诉内容")
    public Result findComplaint(@ApiParam("用户id") @RequestAttribute Long userId,
                                @ApiParam("投诉id") Long complaintId) {
        return complaintService.findComplaint(userId, complaintId);
    }

    @Login
    @PostMapping(value = "/appealIndent")
    @ApiOperation("被投诉人提交申诉")
    @SysLog(value = "被投诉人提交申诉", type = "2")
    public Result appealIndent(@ApiParam("用户id") @RequestAttribute Long userId,
                               @ApiParam("投诉实体") @RequestBody SysComplaint sysComplaint) {
        return complaintService.appealIndent(userId, sysComplaint);
    }

    @Login
    @ApiOperation("用户投诉")
    @SysLog(value = "用户投诉", type = "2")
    @PostMapping(value = "/insertComplaint")
    public Result insertComplaint(@ApiParam("用户id") @RequestAttribute("userId") Long userId,
                                  @ApiParam("投诉实体") @RequestBody SysComplaint sysComplaint) {
        sysComplaint.setUserId(userId);
        return complaintService.insertComplaint(sysComplaint);
    }

    @Login
    @ApiOperation("用户获取投诉列表")
    @GetMapping(value = "/getUserComplaint")
    public Result getUserComplaint(@ApiParam("用户id") @RequestAttribute("userId") Long userId,
                                   @ApiParam("页码") Integer page,
                                   @ApiParam("条数") Integer limit,
                                   @ApiParam("本投诉状态（1可申诉；2申诉中；3申诉未通过；4申诉通过；5用户发起投诉；6用户投诉被拒绝）") Integer complaintState,
                                   @ApiParam("用户名") String userName) {
        return Result.success().put("data", complaintService.getUserComplaint(userId, page, limit, complaintState, userName));
    }

    @Login
    @GetMapping("/getRecycleAppTbOrdersList")
    @ApiOperation("回收人员查看自己被派单的列表")
    public Result getRecycleAppTbOrdersList(@ApiParam("用户id") @RequestAttribute Long userId,
                                            @ApiParam("页码") Integer page,
                                            @ApiParam("条数") Integer limit,
                                            @ApiParam("订单实体") TbOrders tbOrders) {
        tbOrders.setBuyerId(userId);
        return tbOrdersService.getRecycleAppTbOrdersList(page, limit, tbOrders);
    }

    @Login
    @PostMapping("/offlineTransaction")
    @ApiOperation("线下交易")
    public Result offlineTransaction(@ApiParam("用户id") @RequestAttribute Long userId,
                                     @ApiParam("订单实体") TbOrders tbOrders) {
        tbOrders.setBuyerId(userId);
        return tbOrdersService.offlineTransaction(tbOrders);
    }

    @Login
    @ApiOperation("发快递")
    @PostMapping(value = "/sendGoods")
    public Result sendGoods(@ApiParam("订单实体") TbOrders tbOrders,
                            @ApiParam("用户id") @RequestAttribute Long userId) {
        tbOrders.setSellerId(userId);
        return tbOrdersService.sendGoods(tbOrders);
    }

    @Login
    @ApiOperation("根据快递单号查询物流信息")
    @GetMapping(value = "/getLogistics")
    public Result getLogistics(@ApiParam("用户id") @RequestAttribute Long userId,
                               @ApiParam("快递单号") String trackingNumber) {
        if (userId == null) {
            return Result.error("请先登录");
        }
        return tbOrdersService.getLogistics(trackingNumber);
    }

    @GetMapping("/countByRecycleUserId")
    @ApiOperation("统计回收订单的待回收数量")
    @Login
    public Result countByRecycleUserId(@RequestAttribute Long userId) {
        return tbOrdersService.countByRecycleUserId(userId);
    }

}
